<?php

namespace app\wechat\service;

use app\wechat\model\MiniLiveModel;
use Exception;

/**
 * 小程序直播
 *
 * @Author tanruizheng
 * @DateTime 2021-08-17
 */
class LiveService extends MiniService
{
    /**
     * 同步小程序直播列表
     *
     * @Author tanruizheng
     * @DateTime 2021-08-23
     * @return void
     */
    public function sysMiniLive(){
        try {
            $dataList = [];
            $roomList = $this->getRooms(0, 100);
            foreach ($roomList as $item) {
                $data['live_name']      = $item['name'];
                $data['roomid']         = $item['roomid'];
                $data['cover_img']      = $item['cover_img'];
                $data['live_status']    = $item['live_status'];
                $data['start_time']     = $item['start_time'];
                $data['end_time']       = $item['end_time'];
                $data['anchor_name']    = $item['anchor_name'];
                $data['anchor_img']     = $item['anchor_img'];
                $data['total']          = $item['total'];
                $data['browse_num']     = $item['browse_num'];
                $data['app_id']         = $this->appId;
                $dataList[] = $data;
            }
            if(!empty($dataList)){
                //删除旧数据
                MiniLiveModel::where('app_id', $this->appId)->delete();
                //插入新数据
                $MiniLiveModel = new MiniLiveModel();
                $MiniLiveModel->saveAll($dataList);
            }
            return self::createSuccess('同步成功');
        } catch (\Exception $e) {
            return self::createFail($e->getMessage());
        }
    }

    /**
     * 获取微信小程序直播间
     *
     * @Author tanruizheng
     * @DateTime 2021-08-24
     * @param integer $start
     * @param integer $limit
     * @param array $roomList
     * @return void
     */
    public function getRooms($start = 0, $limit = 100, &$roomList = []){
        try {
            $res = $this->getApp()->live->getRooms($start, $limit);
            if($res['errcode'] == 0){
                $roomInfo = $res['roomInfo'];
                foreach ($roomInfo as $key => $value) {
                    $roomList[] = $value;
                }
                if(!empty($roomInfo)){
                    $this->getRooms($start+1, $limit, $roomList);
                }
                return $roomList;
            }else{
                throw new Exception($res['errmsg']);
            }
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 获取小程序直播回放记录
     *
     * @Author tanruizheng
     * @DateTime 2021-08-23
     * @param [type] $roomId
     * @return void
     */
    public function getPlayBack($roomId){
        try {
            $res = $this->getApp()->live->getPlaybacks($roomId);
            if($res['errcode'] == 0){
                $liveReplay = $res['live_replay'];
                return self::createSuccess('success',[
                    'live_replay' => $liveReplay
                ]);
            }else{
                return self::createFail($res['errmsg'],$res);
            }
        } catch (\Exception $e) {
            return self::createFail($e->getMessage());
        }
    }
}